package com.example.springbootrabbitmq.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Sender {
    private static Logger log = LoggerFactory.getLogger(Sender.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send(String msg){
        log.info("send message:"+msg);
        amqpTemplate.convertAndSend(RabbitMQConfig.QUEUE,msg);
    }

    public void sendTopic(String msg){
        log.info("send topic message:"+msg);
        amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE,"topic.key1",msg+1);
        amqpTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE,"topic.key2",msg+2);
    }

    public void sendFanout(String msg){
        log.info("send fanout message:"+msg);
        amqpTemplate.convertAndSend(RabbitMQConfig.FANOUT_EXCHANGE,"",msg);
    }

    public void sendHeaders(String msg){
        log.info("send headers message:"+msg);
        MessageProperties properties=new MessageProperties();
        properties.setHeader("headers1","value1");
        properties.setHeader("headers2","value2");

        Message message=new Message(msg.getBytes(),properties);

        amqpTemplate.convertAndSend(RabbitMQConfig.HEADERS_EXCHANGE,"", message);
    }

}